package com.sykj.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sykj.pojo.Category;
import com.sykj.pojo.Product;
import com.sykj.pojo.ProductVO;
import com.sykj.pojo.User;
import com.sykj.servies.CategoryServies;
import com.sykj.servies.LoginServies;
import com.sykj.servies.ProductServies;
import com.sykj.util.Page;
import com.sykj.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {
    //后台展示商品
    //商品
    @Autowired
    ProductServies productServies;
    //商品分类
    @Autowired
    CategoryServies categoryServies;
    //得到当前登录管理员来判断权限
    @Autowired
    LoginServies loginServies;

    //查询商品表到页面
    @RequestMapping("/list")
    public  String list(Page page, Model model){
        //用mybatis分页插件来拿到分页工具类来查出开始页和总页数
        PageHelper.offsetPage(page.getStart(),page.getCount());
        //拿方法给集合
        List<Product> list=productServies.list();
        //总条数
        int total = (int) new PageInfo<>(list).getTotal();
        //保存传值
        model.addAttribute("list",list);
        model.addAttribute("total",total);
        model.addAttribute("page", page);
        page.setTotal(total);

        //返回给页面
        return "productmodule/product-list";
    }

    //商品修改上线
    @RequestMapping("/enableStatus")
    @ResponseBody
    public String enableStatus(@RequestParam(value = "name") String name){
        return productServies.enableStatus(name);
    }

    //商品修改下线
    @RequestMapping("/stopStatus")
    @ResponseBody
    public String stopStatus(@RequestParam(value = "name") String name){
        return productServies.stopStatus(name);
    }


    //点击模态框增加商品
    @RequestMapping("/productAddUI")
    public String addUI(Model model){
        //得到所有商品类型
        //增加需要引用**什么类型的商品
        List<Category> categoryList = categoryServies.list();
        //得到所有管理员
        //增加到那个管理员
        List<User> userList = loginServies.list();

        model.addAttribute("categoryList",categoryList);
        model.addAttribute("userList",userList);

        return "productmodule/product-add";
    }
    //增加/图片上传的功能
    @RequestMapping("/addProduct")
    public  String add(Product product, HttpSession session, UploadUtil upload)throws  Exception{
        productServies.save(product);

        //上传不为空
        if (upload != null) {
            //获取文件名
            String imageName = product.getId()+".jpg";
            /**
             * 获得session，由这个request去取出和这个request对应的session对象，
             */
            File file = new File(session.getServletContext().getRealPath("/images/product"),imageName);

            System.out.println(session.getServletContext().getRealPath("/images/product"));
            //如果没有改目录就创建该目录
            file.getParentFile().mkdirs();
            //抛异常
            //把获取的图片上传
            upload.getImage().transferTo(file);

            System.out.println("["+product.getId()+","+"images/product/"+imageName+"]");
            //获取图片地址路径
            ProductVO vo = new ProductVO();
            vo.setId(product.getId());
            vo.setImageUrl("images/product/"+imageName);
            //得到图片
            productServies.setImageURL(vo);

            System.out.println(productServies.get(product.getId()));
        }
        //返回商品页，刷新
        return "redirect:list";
    }
    //删除
    @RequestMapping("/deleteProduct")
    public String del(@RequestParam(value = "id")int id,HttpSession session){
        productServies.del(id);
        //得到图片名称
        String imageName = id+".jpg";
        File file = new File(session.getServletContext().getRealPath("/images/product"),imageName);
        //删除图片
        file.delete();
        //重定向到商品页，刷新
        return "redirect:list";
    }

    //点击修改跳转到商品修改界面
    @RequestMapping("/editProduct")
    public String editUI(@RequestParam(value = "id")int id,Model model){
        //获得要修改商品的信息
        Product product = productServies.get(id);
        //保存在modle
        model.addAttribute("product",product);
        System.out.println(product);
        //得到所有分类商品
        List<Category> categoryList = categoryServies.list();
        //得到所有管理员
        List<User> userList = loginServies.list();
        //通过商品id 返回所属分类
        Category categoryByid = productServies.getCategoryByCid(id);
        model.addAttribute("crrentCategory",categoryByid);
        //通过id返回所属商家
        User userById = loginServies.getUserByPid(id);
        model.addAttribute("crrentUser",userById);

        model.addAttribute("categoryList",categoryList);
        model.addAttribute("userList",userList);

        return "productmodule/product-edit";
    }
    //修改商品
    @RequestMapping("/updateProduct")
    public String update(Product product, HttpSession session, UploadUtil upload) throws IOException {
        //商品修改
        productServies.update(product);
        if(upload!=null){
            //修改图片
            String imageName = product.getId()+".jpg";

            File file = new File(session.getServletContext().getRealPath("/images/product"),imageName);
            //如果没有改目录就创建该目录
            file.getParentFile().mkdirs();
            //把获取的图片上传
            upload.getImage().transferTo(file);

            ProductVO vo = new ProductVO();
            vo.setId(product.getId());
            vo.setImageUrl("images/product/"+imageName);

            productServies.setImageURL(vo);

        }
        //重定向返回商品页面，刷新
        return "redirect:list";
    }
}
